﻿Shader "Custom/NewShader" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_BumpMap("BumpMap",2D) = ""{}
		_Sky("Sky",cube)=""{}
		_SelfIllumStrength("SelfIllumStrength",float) = 1
		_RoomReflectionAmount("RoomReflectionAmount",float) = 1
	}
	
	CGINCLUDE
		#include "UnityCG.cginc"
		struct v2f{
			float4 pos:POSITION;
			float2 uv:TEXCOORD;
			float3 viewDir:TEXCOORD1;
			float4 color:TEXCOORD2;
		};
		
		sampler2D _MainTex;
		sampler _BumpMap;
		sampler _Sky;
		float _SelfIllumStrength;
		float _RoomReflectionAmount;
		
		v2f vert(appdata_full i){
			v2f o;
			float4 worldPos = mul(_Object2World,i.vertex);
			o.pos = mul(UNITY_MATRIX_MVP,i.vertex);
			o.viewDir = _WorldSpaceCameraPos - worldPos;
			o.uv = i.texcoord;
			o.color = i.color;
			return o;
		}
		
		float4 frag(v2f i):COLOR{
			float4 tex = tex2D(_MainTex,i.uv);
			float3 bump = UnpackNormal(tex2D(_BumpMap,i.uv));
			float3 reflDir = reflect(i.viewDir,bump);
			float4 reflColor = texCUBE(_Sky,reflDir);
			
			tex *= i.color + tex.a * _SelfIllumStrength;
			tex += reflColor * _RoomReflectionAmount * saturate(tex.b - 0.225);
			return tex;
		}
	ENDCG
	
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		Pass{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			ENDCG
		}
	} 
	FallBack "Diffuse"
}
